import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';

class UrlLauncherPage extends StatefulWidget {
  const UrlLauncherPage({super.key});

  @override
  State<UrlLauncherPage> createState() => _UrlLaunchPagerState();
}

class _UrlLaunchPagerState extends State<UrlLauncherPage> {

  final String url = "https://www.pgyer.com";
  
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text("Flutter demo")),
      body: Center(
        child: Column(
          children: [
            ElevatedButton(
              onPressed: () async {
                const urlTaobao = 'taobao://';
                if (await canLaunchUrl(Uri.parse(urlTaobao))) {
                  final result = await launchUrl(
                    Uri.parse(urlTaobao),
                    mode: LaunchMode.externalApplication
                  );
                  if (kDebugMode) {
                    print(result);
                  }
                }
              },
              child: const Text("打开淘宝"),
            ),
            ElevatedButton(
              onPressed: () async {
                const urlJd = 'baidumap://map';
                if (await canLaunchUrl(Uri.parse(urlJd))) {
                  final result = await launchUrl(
                    Uri.parse(urlJd),
                  );
                  if (kDebugMode) {
                    print(result);
                  }
                }
              },
              child: const Text("打开百度地图"),
            ),
            ElevatedButton(
              onPressed: () async {
                const urlJd = 'androidamap://navi';
                if (await canLaunchUrl(Uri.parse(urlJd))) {
                  final result = await launchUrl(
                    Uri.parse(urlJd),
                  );
                  if (kDebugMode) {
                    print(result);
                  }
                }
              },
              child: const Text("打开高德地图"),
            ),
            ElevatedButton(
              onPressed: () async {
                await launchUrl(
                  Uri.parse(url),
                  mode: LaunchMode.externalApplication
                );
              },
              child: const Text("打开url"),
            ),
          ],
        )
      )
    );
  }
}
